package com.cn.sl.server.api.system.user.util

import org.dromara.hutool.core.codec.binary.Base64
import org.dromara.hutool.core.text.CharSequenceUtil
import org.dromara.hutool.crypto.KeyUtil
import org.dromara.hutool.crypto.SecureUtil
import org.dromara.hutool.crypto.symmetric.SymmetricAlgorithm

/**
 * 密码加密
 */
fun encrypt(password: String): UserPassword {
    val key = KeyUtil.generateKey(SymmetricAlgorithm.DES.value).encoded
    val salt = Base64.encode(key)
    val des = SecureUtil.des(key)
    return UserPassword(
        des.encryptBase64(password),
        salt
    )
}

/**
 * 密码比较
 * @param userEncryptPassword 用户加密密码
 * @param inputPassword 用户输入密码
 * @param salt 用户密码加密用的盐
 */
fun passwordCompare(userEncryptPassword: String, inputPassword: String, salt: String): Boolean {
    val des = SecureUtil.des(Base64.decode(salt))
    return CharSequenceUtil.equals(userEncryptPassword, des.encryptBase64(inputPassword))
}

data class UserPassword(
    /**
     * 加密后密码
     */
    val encryptPassword: String,
    /**
     * 盐
     */
    val salt: String
)